<template>
  <header class="title-bar" :class="titleBarClasses">
    <!-- Left Section -->
    <div class="title-bar__left">
      <!-- Mobile Menu Button -->
      <button
        v-if="ui.isMobile"
        @click="ui.toggleSidebar"
        class="menu-button"
        title="菜单"
      >
        <span class="icon">☰</span>
      </button>
      
      <!-- Chat Info -->
      <div class="chat-info">
        <h1 class="chat-title">{{ chatTitle }}</h1>
        <div class="client-info">
          <span class="client-name">{{ clientName }}</span>
          <span
            v-if="onlineStatus"
            class="online-indicator"
            :class="{ 'online-indicator--active': onlineStatus }"
          >
            <span class="status-dot"></span>
            在线
          </span>
        </div>
      </div>
    </div>
    
    <!-- Right Section -->
    <div class="title-bar__right">
      <!-- Action Buttons -->
      <div class="action-buttons">
        <button
          v-for="action in actions"
          :key="action.id"
          @click="action.onClick"
          class="action-button"
          :title="action.label"
        >
          <span class="icon">{{ getActionIcon(action.icon) }}</span>
        </button>
      </div>
      
      <!-- Client Info Toggle (Desktop) -->
      <button
        v-if="!ui.isMobile"
        @click="ui.toggleClientInfo"
        class="client-info-toggle"
        :class="{ 'client-info-toggle--active': ui.clientInfoVisible }"
        title="客户信息"
      >
        <span class="icon">👤</span>
      </button>
    </div>
  </header>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useUI } from '@/composables'
import type { TitleBarProps } from '@/types'

// Props
defineProps<TitleBarProps>()

// Composables
const ui = useUI()

// Computed properties
const titleBarClasses = computed(() => ({
  'title-bar--mobile': ui.isMobile,
  'title-bar--tablet': ui.isTablet,
  'title-bar--desktop': ui.isDesktop,
  'title-bar--dark': ui.theme === 'dark'
}))

// Methods
const getActionIcon = (iconName: string): string => {
  const icons: Record<string, string> = {
    info: 'ℹ️',
    search: '🔍',
    more: '⋯',
    phone: '📞',
    video: '📹',
    settings: '⚙️',
    close: '✕'
  }
  return icons[iconName] || '•'
}
</script>

<style scoped>
.title-bar {
  height: var(--titlebar-height);
  background-color: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--spacing-lg);
  position: relative;
  z-index: var(--z-sticky);
}

.title-bar--dark {
  background-color: #2a2a2a;
  border-bottom-color: #404040;
}

/* Left Section */
.title-bar__left {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
  flex: 1;
  min-width: 0;
}

.menu-button {
  width: 40px;
  height: 40px;
  border: none;
  background: none;
  color: var(--color-text-primary);
  cursor: pointer;
  border-radius: var(--border-radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color var(--transition-fast);
}

.menu-button:hover {
  background-color: var(--color-surface-hover);
}

.menu-button .icon {
  font-size: var(--font-size-lg);
}

.chat-info {
  flex: 1;
  min-width: 0;
}

.chat-title {
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
  margin: 0 0 var(--spacing-xs) 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.client-info {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  font-size: var(--font-size-sm);
}

.client-name {
  color: var(--color-text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.online-indicator {
  display: flex;
  align-items: center;
  gap: var(--spacing-xs);
  color: var(--color-text-muted);
  font-size: var(--font-size-xs);
}

.online-indicator--active {
  color: var(--color-success);
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: var(--border-radius-full);
  background-color: currentColor;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

/* Right Section */
.title-bar__right {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
}

.action-buttons {
  display: flex;
  gap: var(--spacing-xs);
}

.action-button {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  color: var(--color-text-secondary);
  cursor: pointer;
  border-radius: var(--border-radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
}

.action-button:hover {
  background-color: var(--color-surface-hover);
  color: var(--color-text-primary);
}

.action-button .icon {
  font-size: var(--font-size-base);
}

.client-info-toggle {
  width: 40px;
  height: 40px;
  border: 1px solid var(--color-border);
  background-color: var(--color-surface);
  color: var(--color-text-secondary);
  cursor: pointer;
  border-radius: var(--border-radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
}

.client-info-toggle:hover {
  background-color: var(--color-surface-hover);
  border-color: var(--color-border-dark);
  color: var(--color-text-primary);
}

.client-info-toggle--active {
  background-color: var(--color-primary);
  border-color: var(--color-primary);
  color: var(--color-text-inverse);
}

.client-info-toggle .icon {
  font-size: var(--font-size-base);
}

/* Responsive adjustments */
.title-bar--mobile {
  padding: 0 var(--spacing-md);
}

.title-bar--mobile .chat-title {
  font-size: var(--font-size-base);
}

.title-bar--mobile .client-info {
  font-size: var(--font-size-xs);
}

.title-bar--mobile .action-buttons {
  gap: var(--spacing-xs);
}

.title-bar--mobile .action-button {
  width: 32px;
  height: 32px;
}

/* Focus states */
.menu-button:focus,
.action-button:focus,
.client-info-toggle:focus {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* Loading state */
.title-bar--loading .chat-title {
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: loading 1.5s infinite;
  color: transparent;
  border-radius: var(--border-radius-sm);
}

.title-bar--dark.title-bar--loading .chat-title {
  background: linear-gradient(90deg, #404040 25%, #505050 50%, #404040 75%);
  background-size: 200% 100%;
}

@keyframes loading {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* Print styles */
@media print {
  .title-bar {
    border-bottom: 2px solid #000;
    background: white !important;
  }
  
  .menu-button,
  .action-buttons,
  .client-info-toggle {
    display: none !important;
  }
}
</style>